package com.ljkj.cordial.chat.activity;


import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.lanjiu.lib.business.FileTransmissionManager;
import com.lanjiu.lib.imclient.dto.user.UserDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.dao.UserTableBean;
import com.ljkj.cordial.chat.ui.AppEventActivity;
import com.ljkj.cordial.chat.ui.views.SplashView;
import com.ljkj.cordial.chat.util.DeleteHistoryUtil;
import com.ljkj.cordial.chat.widget.CommonHintDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 启动页
 */
public class SplashActivity extends AppEventActivity<SplashView> implements SplashView.SplashCallback {


    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.frame_bottom)
    FrameLayout frameBottom;

    private boolean isGrant;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {
//        setAndroidNativeLightStatusBar(this, true);
        ButterKnife.bind(this);
        setToolbarVisibility(View.GONE);
        getPermission();
        FileTransmissionManager.shutdown();

    }


    private void getPermission() {
        checkPermission(new CheckPermListener() {
                            /**
                             *
                             */
                            @Override
                            public void superPermission() {
                                isGrant = true;

//                                FriendTable.clearOverTimeTourist();
                                UserTableBean u = ChatApp.getInstance().get();
                                if (u != null) {
//                                    if (!TextUtils.isEmpty(u.getTemporaryId())) {
//                                        long lastQuitTime = (long) SharedUtils.get(mContext, u.getTemporaryId(), 0L);
//                                        if (lastQuitTime != 0L && System.currentTimeMillis() - lastQuitTime > 24 * 60 * 60 * 1000) {
//                                            DeleteHistoryUtil.getInstance().deleteVisitorAll(new DeleteHistoryUtil.OnDeleteFinishListener() {
//                                                @Override
//                                                public void onFinish() {
////                                                    SharedUtils.remove(mContext, visitorId);
////                                                    SharedUtils.remove(mContext, Canstants.KEY_VisitorId);
////                                                    SharedUtils.put(mContext, Canstants.KEY_UseType, UserTableBean.TYPE_VISITOR);
////                                                    openActivity(ChatMainActivity.class);
////                                                    finish();
//                                                }
//                                            });
//                                            return;
//                                        }
//                                    } else
                                    if (!TextUtils.isEmpty(u.getUserId())) {
                                        if (ChatApp.getInstance().isExit()) {
//                                            frameBottom.setVisibility(View.VISIBLE);
                                            jumpToLogin();
                                            return;
                                        }
                                        DeleteHistoryUtil.getInstance().clear(mContext, (clearCyc, isClear) -> {
                                            openActivity(ChatMainActivity.class);
                                            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                                            finish();
                                        });

                                        return;
                                    }

                                } else {
                                    jumpToLogin();
                                }
//                                frameBottom.setVisibility(View.VISIBLE);
//                                LoginActivity.jumpTo((Activity) mContext, Canstants.STATE_REGISTER_LOGIN);
//                                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//                                finish();
                            }

                        }, R.string.permission,
                Manifest.permission.CAMERA, Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO);
    }

    public void jumpToLogin() {
        frameBottom.postDelayed(() -> DeleteHistoryUtil.getInstance().clear(mContext, (clearCyc, isClear) -> {
            LoginActivity.jumpTo((Activity) mContext, Canstants.STATE_REGISTER_LOGIN);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

            finish();
        }), 1000);
    }

    @OnClick({R.id.tv_login, R.id.tv_register})
    public void onViewClicked(View view) {
        if (!isGrant) {
            getPermission();
            return;
        }
        switch (view.getId()) {
            case R.id.tv_login:
//                String visitorId = (String) SharedUtils.get(mContext, Canstants.KEY_VisitorId, "");
//                if (TextUtils.isEmpty(visitorId)) {
//                    obtainView().doTouristsLogin();
//                } else {
//                    SharedUtils.put(mContext, Canstants.KEY_UseType, UserTableBean.TYPE_VISITOR);
//                    openActivity(ChatMainActivity.class);
//                    finish();
//                }
//                LoginActivity.jumpTo((Activity) mContext, Canstants.STATE_REGISTER_LOGIN);
                DeleteHistoryUtil.getInstance().clear(mContext, (clearCyc, isClear) -> {
                    LoginActivity.jumpTo((Activity) mContext, Canstants.STATE_REGISTER_LOGIN);
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                });

                break;
            case R.id.tv_register:
//                String userId = (String) SharedUtils.get(mContext, Canstants.KEY_UserId, "");
//                if (TextUtils.isEmpty(userId)) {
//                    LoginActivity.jumpTo(this, Canstants.STATE_REGISTER_LOGIN);
//                } else {
//                    SharedUtils.put(mContext, Canstants.KEY_UseType, UserTableBean.TYPE_REGISTER);
//                    openActivity(ChatMainActivity.class);
//                    finish();
//                }
                DeleteHistoryUtil.getInstance().clear(mContext, (clearCyc, isClear) -> {
                    RegisterActivity.jumpTo((Activity) mContext);
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                });

                break;
        }
    }


    @Override
    protected SplashView bindNettyMessageView() {
        return new SplashView(this);
    }

    @Override
    public void onLoginCallback(int code, String msg, UserDto data) {
        if (IMSContacts.ResponseCode.SUCCESS == code && data != null) {
            if (!TextUtils.isEmpty(data.getUserId())) {
                new CommonHintDialog(this)
                        .visiableWarn()
                        .setTitle("游客登录")
                        .setContent(String.format("临时ID：%s\n退出聊天时回收清除临时ID以及信息", data.getUserId()))
                        .setLeftSubmit("继续登录", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
//                                SharedUtils.put(mContext, Canstants.KEY_VisitorId, data.getUserId());
//                                SharedUtils.put(mContext, Canstants.KEY_UseType, UserTableBean.TYPE_VISITOR);
//                                SharedUtils.put(mContext, Canstants.KEY_Nickname, data.getNickname());
//                                SharedUtils.put(mContext, Canstants.KEY_Avatar, data.getAvatar());
                                UserTableBean user = new UserTableBean();
                                user.insert(data);
                                ChatMainActivity.jumpTo(SplashActivity.this, false);
                                finish();
                            }
                        })
                        .setRightSubmit("我要注册", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                openActivity(RegisterActivity.class);
                                finish();
                            }
                        })
                        .show();
            }

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Canstants.REQUEST_CODE_BACK && resultCode == Canstants.REQUEST_CODE_BACK) {
            finish();
        }
    }

    @Override
    public void finish() {
        ChatApp.getInstance().removeActivity(SplashActivity.this);
        super.finish();
    }
}
